﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using DATN.Common;
using DATN.Entities;

namespace DATN.DataAccess
{
    public class UserDataAccess
    {
        private SqlConnection conn;
        private SqlCommand cmd;
        public static User ParseToEntity(IDataRecord dataRecord)
        {
            return new User
            {
                UserName = dataRecord["UserName"].ToStringOrDefault(),
                Role = dataRecord["Role"].ToStringOrDefault().ToNumeric(),
                Email = dataRecord["Email"].ToStringOrDefault(),
                Status = dataRecord["Status"].ToStringOrDefault(),
                Ma_nhan_vien = dataRecord["Ma_nhan_vien"].ToStringOrDefault()
            };
        }

        //lấy thông tin user
        public User GetUser(string userName, string password)
        {

            conn = new SqlConnection(AppConfig.ConnectionString);
            string truyVan = "Select * from tblUser where UserName = '" + userName + "' and Password = '" + password + "'";
            SqlCommand cmd = new SqlCommand(truyVan, conn);
            conn.Open();
            using (IDataReader iReader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                if (iReader.Read())
                {
                    return ParseToEntity(iReader);
                }
            }
            conn.Close();
            return null;
        }

        //Lấy thông tin tất cả user
        public Collection<User> GetAllUser()
        {
            conn = new SqlConnection(AppConfig.ConnectionString);
            string truyvan = "Select * from tblUser";
            cmd = new SqlCommand(truyvan, conn);
            conn.Open();

            using (IDataReader iDataReader = cmd.ExecuteReader())
            {
                Collection<User> collection = new Collection<User>();

                while (iDataReader.Read())
                {
                    collection.Add(ParseToEntity(iDataReader));
                }
                return collection;
            }
        }

        //Kiểm tra mail trong database
        public int CheckMail(string email)
        {
            conn = new SqlConnection(AppConfig.ConnectionString);
            string truyVan = "Select count(*) as Counter from tblUser where Email= '" + email + "'";
            cmd = new SqlCommand(truyVan, conn);
            conn.Open();
            using (IDataReader iDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                int Counter = 0;
                while (iDataReader.Read())
                {
                    Counter = iDataReader["Counter"].ToStringOrDefault().ToNumeric();
                }
                return Counter;
            }
        }

        //Update password theo email
        public bool UpdatePasswordByEmail(string email, string password)
        {
            conn = new SqlConnection(AppConfig.ConnectionString);
            string truyVan = "Update tblUser set Password = '" + password + "' where Email = '" + email + "'";
            cmd = new SqlCommand(truyVan, conn);
            conn.Open();
            return cmd.ExecuteNonQuery() > 0;
        }

        //Update password theo email
        public bool UpdatePasswordByUser(string user, string password)
        {
            conn = new SqlConnection(AppConfig.ConnectionString);
            string truyVan = "Update tblUser set Password = '" + password + "' where UserName = '" + user + "'";
            cmd = new SqlCommand(truyVan, conn);
            conn.Open();
            return cmd.ExecuteNonQuery() > 0;
        }

        //Them User
        public bool InsertUser(string user, string pass, string email, int role, string maNV)
        {
            conn = new SqlConnection(AppConfig.ConnectionString);
            string truyVan = "insert into tblUser(UserName,Password,Email,Role,Ma_nhan_vien)"
                                + "values('"+user+"','"+pass+"','"+email+"',"+role+",'"+maNV+"')";
            cmd = new SqlCommand(truyVan, conn);
            conn.Open();
            return cmd.ExecuteNonQuery() > 0;
        }
    }
}
